package cn.qiao.web.exception;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.PrintWriter;

@RestControllerAdvice
public class ExceptionsHandler {

    private static final Logger log = LoggerFactory.getLogger(ExceptionsHandler.class);
    @ExceptionHandler(Throwable.class)
     public void exceptionHandler(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Throwable throwable) {
        log.error("", throwable);
        httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        try (PrintWriter writer = httpServletResponse.getWriter()) {
            writer.write("internal server error");
        } catch (Exception e) {
            log.error("", e);
        }
     }
}
